package com.tx.core.mybatis.conditions;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 更新条件
 *    在非MybatisPlus中，由于是借助生成sqlMap来进行实现
 *    不对having,groupby提供支持，虽然可以实现，但是需要考虑的情形太多
 *    在当前版本不支持having及groupby
 *
 * @author PengQingyang
 * 创建时间 [2024/3/31]
 */
public class UpdaterWrapper<T> extends AbstractWrapper<T, String, UpdaterWrapper<T>>
        implements Update<UpdaterWrapper<T>, String> {

    private final List<String> sqlSet;

    public UpdaterWrapper() {
        // 如果无参构造函数，请注意实体 NULL 情况 SET 必须有否则 SQL 异常
        this(null);
    }

    public UpdaterWrapper(T entity) {
        super.setEntity(entity);
        super.initNeed();

        this.sqlSet = new ArrayList<>();
    }

    private UpdaterWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
                           Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
                           SharedString paramAlias,
                           SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.paramAlias = paramAlias;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(Constants.COMMA, sqlSet);
    }

    @Override
    public UpdaterWrapper<T> set(boolean condition, String column, Object val, String mapping) {
        return maybeDo(condition, () -> {
            String sql = formatParam(mapping, val);
            sqlSet.add(column + Constants.EQUALS + sql);
        });
    }

    @Override
    public UpdaterWrapper<T> setSql(boolean condition, String setSql, Object... params) {
        if (condition && StringUtils.isNotBlank(setSql)) {
            sqlSet.add(formatSqlMaybeWithParam(setSql, params));
        }
        return typedThis;
    }

    @Override
    protected UpdaterWrapper<T> instance() {
        return new UpdaterWrapper<>(getEntity(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(),
                paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSet.clear();
    }

    /**
     * 构建器<br/>
     * @return
     * @param <T>
     */
    public static <T> UpdaterWrapperBuilder<T> builder() {
        return new UpdaterWrapperBuilder<T>();
    }

    /**
     * 构建器定义<br/>
     * @return
     * @param <T>
     */
    public static class UpdaterWrapperBuilder<T> {

        private UpdaterWrapperBuilder() {}

        public UpdaterWrapper<T> build() {
            return new UpdaterWrapper();
        }
    }
}